class MyBoy {
  constructor(name) {
    this.name = name;
    this.taskList = [];

    setTimeout(() => {
      this.next();
    }, 0);
  }

  sayHi() {
    this.taskList.push(() => {
      console.log(`hello 我是${this.name}`);
      this.next();
    });
    return this;
  }
  play(game) {
    this.taskList.push(() => {
      console.log(`我在玩${game}`);
      this.next();
    });
    return this;
  }
  sleep(time) {
    this.taskList.push(() => {
      setTimeout(() => {
        // do nothing
        this.next();
      }, time);
    });
    return this;
  }
  next() {
    const fn = this.taskList.shift();
    fn && fn();
  }
}

const tom = new MyBoy("tom");

// tom.sayHi().play("basketball").sleep(2000);
tom.sayHi().sleep(2000).play("basketball").sleep(3000).play("volleyball"); // TypeError cannot read property 'play' of undefined
